package ed.u8.ej1;

public class Cadena {
	
	private String cadena;
	
	public Cadena(String cadena) {
		this.cadena=cadena;
	}

	public void cDelante(String cadena) {
		this.cadena=this.cadena+cadena;
	}

	public void cDetras(String cadena) {
		this.cadena=cadena+this.cadena;
	}

	public void quitar(char c) {
		String cadenaNueva="";
		for(int i=0;i<cadena.length();i++){
			if(cadena.charAt(i)!=c){
				cadenaNueva=cadenaNueva+cadena.charAt(i);
			}
		}
		this.cadena=cadenaNueva;
	}

	public void quitar(char c, int n) throws NumCaracteresException {
		String cadenaNueva="";
		int contCaracteres=0;
		int numCaracteres=0;
		for(int i=0;i<cadena.length();i++){
			if(cadena.charAt(i)==c){
				numCaracteres++;
			}
		}
		if(n<=numCaracteres){
			for(int i=0;i<cadena.length();i++){
				if(cadena.charAt(i)!=c){
					cadenaNueva=cadenaNueva+cadena.charAt(i);
				}else{
					if(contCaracteres<n){
						contCaracteres++;
					}else{
						cadenaNueva=cadenaNueva+cadena.charAt(i);
					}
				}
			}
			this.cadena=cadenaNueva;
		}
		else{
			throw new NumCaracteresException();
		}
	}

	public String getCadena() {
		return cadena;
	}
	
}
